<?php
class SocialOrigin_1 extends IOSocial{
	protected $app_id = 430040;
	protected $api_key = "686f8be492ff9f3c60b1b1f4c3e70525";
	protected $api_secret_key = "3cbdcb6b8c07f0580c87e243c4ac6ad0";
	protected $api_link			= "http://www.appsmail.ru/platform/api";
	
	public function __construct($api = Array()){
		$this->secure 		= 1;
		$this->settings 	= $api;
		$this->hostData 	= $this->extractHostData($this->api_link);
		if(!empty($_SESSION["api"])){
			$this->_initSessionApi();
		}
	}
	
	function _initSessionApi(){
		$this->settings 	= $_SESSION["api"];
		$this->viewer_id 	= $this->settings["viewer_id"];
	}
	
	
	public function checkAuthorization(){
		//if($this->==md5($this->app_id.'_'.$this->vid.'_'.$this->api_secret_key)){
		if($this->settings["authentication_key"] = md5($this->app_id . '_' . $this->settings["viewer_id"] . '_' . $this->api_secret_key)){
			return true;
		}else{
			return false;
		}

	}
	
	public function getProfile( $uids = Array() ){
		$this->secure = 1;
		$this->request["uids"] = implode(",", $uids);
		$this->method = "users.getInfo";
		$req = $this->getApiRequest( $this->expire_time );	
		$result = array();
		if(is_object($req) or is_array($req) ){
			foreach($req as $value){
				$result[$value["uid"]] = $value;
			}
		}
		return $result;	
	}
	
	
	public function getFriends( $uid ){
		
		$this->request['session_key'] = $this->settings["session_key"];
		$this->request['ext'] = 1;
		$this->method = "friends.get";
		$req = $this->getApiRequest( $this->expire_time );
		
		$result = array();
		if(is_object($req) or is_array($req) ){
			foreach($req as $value){
				$result[$value["uid"]] = $value;
			}
		}
		return $result;
	}
	
	
	
	
	
	/**
	* Отправляет уведомление пользователю.
	*/
	public function Notification($uids = Array(), $message){
		$this->secure = 1;
		$this->request['uids'] = implode(",",$uids);
		$this->request['text'] = $message;
		$this->method = "notifications.send";
		return $this->getApiRequest( 0 );
	}

	protected function genSignature( $request_params = Array()){
	 	$signature = "";
	    foreach ($request_params as $key => $value) {
	    	$signature .= $key."=".$request_params[$key];
	    }
	    
		#if($this->secure == 1){
			$signature = $signature . $this->api_secret_key;
		#}else{
		#	$signature = $this->viewer_id . $signature . $this->api_key;
		#}
		return md5($signature);
	 }


	protected function getApiRequest( $expir = 600){
	# Настройки соединения.
		$request_params=Array();
		if(count($this->request)>0){
		    foreach($this->request as $key => $value){
		    	$request_params[$key]=$value;
		    }
	    }

	    $request_params['app_id'] = $this->app_id;
	    $request_params['secure'] = 1;
	    $request_params['format'] = "JSON";
	    $request_params['method'] = $this->method;
	    
	    ksort($request_params);
		
		$request_params["sig"] = $this->genSignature($request_params);
		//echo "<pre>";
		//print_r($request_params);
	    $mcKey = $this->generateMCKey($request_params);
	    # Пытаемся получить с запроса
		$result = DB::getMCH()->get($mcKey);
		//if($result === false or $expir == 0){

			$comon=new HtmlSource();
		    $comon->host	= "www.appsmail.ru";
		    $comon->page	= "/platform/api";
		    $comon->referer	= "http://my.mail.ru/";
		    $comon->strip_responseheader = true;
		    # Формируем тело запроса
		    foreach ($request_params as $key => $value){
	    		$comon->addPostVar($key,$value);
		    }
		    $result = $comon->getSource();
		    if($expir > 0){
	   			db::getMCH()->set( $mcKey, $result, 0, $expir);
			}
			unset($comon->postvars);
		//}
		//var_dump($result);
	   	$result=json_decode($result, true);
	   	$this->request = Array();
	   	$this->method = null;
		//var_dump($result);
	    return $this->_objectToArray($result);

	}
}
?>
